package com.splic.web.controller.product;

import com.splic.common.annotation.Log;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.AjaxResult;
import com.splic.common.core.page.TableDataInfo;
import com.splic.common.enums.BusinessType;
import com.splic.common.utils.StringUtils;
import com.splic.common.utils.poi.ExcelUtil;
import com.splic.product.domain.VoiceProvider;
import com.splic.product.service.VoiceProviderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 语音转写Controller
 * 
 * @author singe
 * @date 2023-08-16
 */
@Controller
@RequestMapping("/product/provider")
public class VoiceProviderController extends BaseController
{
    private String prefix = "product/provider";

    @Autowired
    private VoiceProviderService voiceProviderServiceImpl;

    @RequiresPermissions("product:provider:view")
    @GetMapping()
    public String provider()
    {
        return prefix + "/list";
    }

    /**
     * 查询语音转写列表
     */
    @RequiresPermissions("product:provider:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VoiceProvider voiceProvider)
    {
        startPage();
        List<VoiceProvider> list = voiceProviderServiceImpl.selectVoiceProviderList(voiceProvider);
        return getDataTable(list);
    }

    /**
     * 新增语音转写
     */
    @GetMapping(value = { "/form/{id}", "/form" })
    public String form(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id)){
            VoiceProvider voiceProvider = voiceProviderServiceImpl.selectVoiceProviderById(id);
            mmap.put("voiceProvider", voiceProvider);
            mmap.put("languagesChecked", Arrays.stream(voiceProvider.getVoiceLanguages().split(",")).collect(Collectors.toList()));
            mmap.put("samplingChecked", Arrays.stream(voiceProvider.getVoiceSampling().split(",")).collect(Collectors.toList()));
            mmap.put("bitChecked", Arrays.stream(voiceProvider.getVoiceBit().split(",")).collect(Collectors.toList()));
            mmap.put("trackChecked", Arrays.stream(voiceProvider.getVoiceTrack().split(",")).collect(Collectors.toList()));
            mmap.put("formatChecked", Arrays.stream(voiceProvider.getVoiceFormat().split(",")).collect(Collectors.toList()));
        }else{
            mmap.put("voiceProvider", new VoiceProvider());
            mmap.put("languagesChecked", new ArrayList<>());
            mmap.put("samplingChecked", new ArrayList<>());
            mmap.put("bitChecked", new ArrayList<>());
            mmap.put("trackChecked", new ArrayList<>());
            mmap.put("formatChecked", new ArrayList<>());
        }
        return prefix + "/form";
    }


    /**
     * 新增保存语音转写
     */
    @RequiresPermissions("product:provider:edit")
    @Log(title = "语音转写", businessType = BusinessType.INSERT)
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(VoiceProvider voiceProvider)
    {
        return toAjax(voiceProviderServiceImpl.saveOrUpdate(voiceProvider));
    }

    /**
     * 删除
     */
    @RequiresPermissions("product:provider:remove")
    @Log(title = "语音转写", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long id)
    {
        return toAjax(voiceProviderServiceImpl.deleteById(id));
    }


    /**
     * 导出语音转写列表
     */
    @RequiresPermissions("product:provider:export")
    @Log(title = "语音转写", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VoiceProvider voiceProvider)
    {
        List<VoiceProvider> list = voiceProviderServiceImpl.selectVoiceProviderList(voiceProvider);
        ExcelUtil<VoiceProvider> util = new ExcelUtil<VoiceProvider>(VoiceProvider.class);
        return util.exportExcel(list, "语音转写数据");
    }

}
